package com.android.zmusic.ui;

import com.android.zmusic.interfaces.IOnSliderHandleViewClickListener;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SlidingDrawer;

public class MySlidingDrawer extends SlidingDrawer {
    
	//抽屉行为控件ID
	private int mhandleId = 0 ;
	//handle中其他控件的ID
	private int[] mTouchableIds = null ;
	
    private IOnSliderHandleViewClickListener mTouchViewClickListener ;
    
 
	public int[] getmTouchableIds() {
		return mTouchableIds;
	}

	public void setmTouchableIds(int[] mTouchableIds) {
		this.mTouchableIds = mTouchableIds;
	}

	public int getMhandleId() {
		return mhandleId;
	}

	public void setMhandleId(int mhandleId) {
		this.mhandleId = mhandleId;
	}
	
	

	public void setmTouchViewClickListener(
			IOnSliderHandleViewClickListener mTouchViewClickListener) {
		this.mTouchViewClickListener = mTouchViewClickListener;
	}

	/**
	 * 获取控件所在的屏幕区域
	 * @param view
	 * @return
	 */
    public Rect getRectOnScreen(View view) {
    	
    	Rect rect = new Rect();
    	int[] location = new int[2];
    	View parent = view;
    	if(view.getParent() instanceof View) {
    		parent = (View) view.getParent();
    	}
    	parent.getLocationOnScreen(location);
    	view.getHitRect(rect);
    	rect.offset(location[0], location[1]);
    	
    	return rect;
    }
    
    
    public boolean OnInterceptTouchEvent(MotionEvent event) {
    	//将触摸的位置转换为屏幕的坐标
    	int[] location = new int[2];
    	int x = (int) event.getX();
    	int y = (int) event.getY();
    	this.getLocationOnScreen(location);
    	x += location[0];
    	y += location[1];
    	
    	if(mTouchableIds != null) {
    		
    		for(int id : mTouchableIds) {
    			
    			View view = findViewById(id);
    			if(view.isShown()) {
    		      
    				Rect rect  = getRectOnScreen(view);
    				if(rect.contains(x,y)) {
    					
    					if(event.getAction() == MotionEvent.ACTION_DOWN) {
    						
    						if(mTouchViewClickListener != null) {
    							mTouchViewClickListener.onViewClick(view);
    						}
    						
    					}
    				return true;
    				}
    				
    			}
    		}
    	}
    	
    	
    	//抽屉行为控件
    	if(event.getAction() == MotionEvent.ACTION_DOWN && mhandleId != 0) {
    		
    		View view = findViewById(mhandleId);
    		Rect rect = getRectOnScreen(view);
    		if(rect.contains(x,y)) {
    			//点击抽屉控件时交由系统处理
    			return super.onInterceptTouchEvent(event);
    		}
    	}else {
    		return false;
    	}
    	
    	return super.onInterceptTouchEvent(event);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	// TODO Auto-generated method stub
    	return super.onTouchEvent(event);
    }

	
	public MySlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public MySlidingDrawer(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	

}
